package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.aliyuncs.exceptions.ClientException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.vo.EnterpriseInfoVO;
import com.ruoyi.system.mapper.GwEnterpriseInfoMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GwStaff;
import com.ruoyi.system.service.IGwStaffService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 员工管理Controller
 *
 * @author ruoyi
 * @date 2025-04-27
 */
@Api(description = "员工管理")
@RestController
@RequestMapping("/system/staff")
public class GwStaffController extends BaseController {
    @Autowired
    private IGwStaffService gwStaffService;
    @Resource
    private GwEnterpriseInfoMapper gwEnterpriseInfoMapper;

    /**
     * 查询员工管理列表
     */
    @ApiOperation("查询员工管理列表")
//    @PreAuthorize("@ss.hasPermi('system:staff:list')")
    @GetMapping("/list")
    public TableDataInfo list(GwStaff gwStaff) {
        startPage();
        List<GwStaff> list = gwStaffService.selectGwStaffList(gwStaff);
        return getDataTable(list);
    }

    @ApiOperation("查询员工管理列表")
    @GetMapping("/getList")
    public AjaxResult getList(GwStaff gwStaff) {
        if (ObjectUtils.isEmpty(SecurityUtils.getLoginUser()) || ObjectUtils.isEmpty(SecurityUtils.getLoginUser().getEnterpriseId())) {
            return success(new ArrayList<>());
        }
        List<GwStaff> list = gwStaffService.selectGwStaffList(gwStaff);

        EnterpriseInfoVO enterpriseInfo = gwEnterpriseInfoMapper.getLoginEnterpriseInfo(SecurityUtils.getLoginUser().getEnterpriseId());
        GwStaff gwStaff0 = new GwStaff();
        gwStaff0.setId(0L);
        gwStaff0.setStaffName(enterpriseInfo.getEnterpriseName());
        list.add(0, gwStaff0);
        return success(list);
    }

    /**
     * 导出员工管理列表
     */
//    @PreAuthorize("@ss.hasPermi('system:staff:export')")
    @Log(title = "员工管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GwStaff gwStaff) {
        List<GwStaff> list = gwStaffService.selectGwStaffList(gwStaff);
        ExcelUtil<GwStaff> util = new ExcelUtil<GwStaff>(GwStaff.class);
        util.exportExcel(response, list, "员工管理数据");
    }

    /**
     * 获取员工管理详细信息
     */
    @ApiOperation("获取员工管理详细信息")
//    @PreAuthorize("@ss.hasPermi('system:staff:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(gwStaffService.selectGwStaffById(id));
    }

    /**
     * 获取员工管理详细信息
     */
    @ApiOperation("获取员工管理详细信息")
//    @PreAuthorize("@ss.hasPermi('system:staff:query')")
    @GetMapping(value = "/query/{id}")
    public AjaxResult queryInfo(@PathVariable("id") String id) {
        return success(gwStaffService.selectGwStaffById(id));
    }

    /**
     * 新增员工管理
     */
    @ApiOperation("新增员工管理")
//    @PreAuthorize("@ss.hasPermi('system:staff:add')")
    @Log(title = "员工管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GwStaff gwStaff) {
        return toAjax(gwStaffService.insertGwStaff(gwStaff));
    }

    @ApiOperation("发送给员工填写")
//    @PreAuthorize("@ss.hasPermi('system:staff:add')")
    @Log(title = "员工管理", businessType = BusinessType.INSERT)
    @PostMapping("/addByStaff")
    public AjaxResult addByStaff(@RequestBody GwStaff gwStaff) throws ClientException, WxErrorException {
        return gwStaffService.addByStaff(gwStaff);
    }

    /**
     * 修改员工管理
     */
    @ApiOperation("修改员工管理")
    //@Log(title = "员工管理", businessType = BusinessType.UPDATE)
    @PostMapping("/editByStaff")
    public AjaxResult editByStaff(@Valid @RequestBody GwStaff gwStaff) {
        return toAjax(gwStaffService.editByStaff(gwStaff));
    }

    /**
     * 修改员工管理
     */
    @ApiOperation("修改员工管理")
    //@PreAuthorize("@ss.hasPermi('system:staff:edit')")
    @Log(title = "员工管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GwStaff gwStaff) {
        return toAjax(gwStaffService.updateGwStaff(gwStaff));
    }

    /**
     * 删除员工管理
     */
    @ApiOperation("删除员工管理")
//    @PreAuthorize("@ss.hasPermi('system:staff:remove')")
    @Log(title = "员工管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(gwStaffService.deleteGwStaffByIds(ids));
    }
}
